<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Model\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function getIndex()
    {
        return view('admin.user.index');
    }

    public function getSearch()
    {
        $users = User::get();
        return $this->json_success($users);
    }

    public function getUser(Request $request)
    {
        $id = $request->input('id');
        $user = User::find($id);
        if (!$user) {
            abort(404);
        }
        return view('admin.user.user', [
            'user' => $user
        ]);


    }

    public function getCreateUser()
    {
        return view('admin.user.create');

    }

    public function postCreateUser(CreateUserRequest $request)
    {
        $name = $request->input('name');
        $password = $request->input('password');
        $phone = $request->input('phone');
        $email = $request->input('email');

        $user = User::getByName($name);
        if ($user) {
            return $this->json_error('用户名已存在', 1001);
        }

        $user = User::getByPhone($phone);
        if ($user) {
            return $this->json_error('手机号已存在', 1002);
        }

        $user = User::getByEmail($email);
        if ($user) {
            return $this->json_error('邮箱已存在', 1003);
        }

        $user = new User();
        $user->name = $name;
        $user->password = strtoupper(md5($password));
        $user->email = $email;
        $user->phone = $phone;
        $user->is_admin = 0;
        $user->save();
        return $this->json(1, $user->toArray());
    }

    public function getEditUser(Request $request)
    {
        $id = $request->input('id');
        $user = User::find($id);
        if (!$user) {
            abort(404);
        }
        return view('admin.user.edit', [
            'user' => $user
        ]);
    }

    public function postEditUser(Request $request)
    {
        $id = $request->input('id');
        $name = $request->input('name');
        $new_password = $request->input('new_password');
        $phone = $request->input('phone');
        $email = $request->input('email');

        $user = User::getByName($name);
        if ($user && $user->id != $id) {
            return $this->json_error('用户名已存在', 1001);
        }

        $user = User::getByPhone($phone);
        if ($user && $user->id != $id) {
            return $this->json_error('手机号已存在', 1002);
        }

        $user = User::getByEmail($email);
        if ($user && $user->id != $id) {
            return $this->json_error('邮箱已存在', 1003);
        }

        $user = User::find($id);
        if (!$user) {
            return $this->json_error('编辑的用户不存在', 1004);
        }
        $user->name = $name;
        if ($new_password != '') {
            $user->password = strtoupper(md5($new_password));
        }
        $user->email = $email;
        $user->phone = $phone;
        $user->is_admin = 0;
        $user->save();
        return $this->json(1, $user->toArray());

    }

    public function postDeleteUser(Request $request)
    {
        $id = $request->input('id');
        $user = User::find($id);
        if (!$user) {
            return $this->json_error('删除的用户不存在', 1004);
        }
        $user->delete();
        return $this->json_success();
    }


}
